<?php 
/*
 module:		提现审核管理
 create_time:	2022-04-08 15:38:22
 author:	陕西格创
 contact:		
*/

namespace app\api\controller;

use app\api\service\ZhBalanceService;
use app\api\service\ZhWithdrawalExamineService;
use app\api\model\ZhWithdrawalExamine as ZhWithdrawalExamineModel;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;
use app\api\model\Member as MemberModel;
use app\api\model\SysSite;
use app\api\model\ZhBalanceLog as ZhBalanceLogModel;

class ZhWithdrawalExamine extends Common {


	/**
	* @api {get} /ZhWithdrawalExamine/index 01、首页数据列表
	* @apiGroup ZhWithdrawalExamine
	* @apiVersion 1.0.0
	* @apiDescription  首页数据列表

	* @apiHeader {String} Authorization 用户授权token
	* @apiHeaderExample {json} Header-示例:
	* "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"

	* @apiParam (输入参数：) {int}     		[limit] 每页数据条数（默认20）
	* @apiParam (输入参数：) {int}     		[page] 当前页码
	* @apiParam (输入参数：) {string}		[order_sn] 订单号 
	* @apiParam (输入参数：) {int}			[status] 状态 待审核|0|warning,同意|1|success

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码 201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.data 返回数据
	* @apiParam (成功返回参数：) {string}     	array.data.list 返回数据列表
	* @apiParam (成功返回参数：) {string}     	array.data.count 返回数据总数
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":""}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" 201","msg":"查询失败"}
	*/
	function index(){
		$limit  = $this->request->get('limit', 20, 'intval');
		$page   = $this->request->get('page', 1, 'intval');
        $uid = $this->request->uid;

		$where = [];
        $where['u_id'] = $uid;
		$where['order_sn'] = $this->request->get('order_sn', '', 'serach_in');
        $status = $this->request->get('status', '-1', 'serach_in');
        if($status > -1){
            $where['status'] = $status;
        }
		$field = '*';
		$orderby = 'id desc';

		$res = ZhWithdrawalExamineService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
		return $this->ajaxReturn($this->successCode,'返回成功',htmlOutList($res));
	}

	/**
	* @api {get} /ZhWithdrawalExamine/view 02、查看详情
	* @apiGroup ZhWithdrawalExamine
	* @apiVersion 1.0.0
	* @apiDescription  查看详情
	
	* @apiParam (输入参数：) {string}     		id 主键ID

	* @apiHeader {String} Authorization 用户授权token
	* @apiHeaderExample {json} Header-示例:
	* "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码 201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.data 返回数据详情
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":""}
	* @apiErrorExample {json} 02 失败示例
	* {"status":"201","msg":"没有数据"}
	*/
	function view(){
		$data['id'] = $this->request->get('id','','serach_in');
		$field='id,nickname,order_sn,price,status,createtime';
		$res  = checkData(ZhWithdrawalExamineModel::field($field)->where($data)->find());
		return $this->ajaxReturn($this->successCode,'返回成功',$res);
	}

	/**
	* @api {post} /ZhWithdrawalExamine/add 添加提现申请记录
	* @apiGroup ZhWithdrawalExamine
	* @apiVersion 1.0.0
	* @apiDescription  添加

	* @apiHeader {String} Authorization 用户授权token
	* @apiHeaderExample {json} Header-示例:
	* "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"
	* @apiParam (输入参数：) {string}			wxapp_id 平台id 
	* @apiParam (输入参数：) {int}				u_id 用户id 
	* @apiParam (输入参数：) {string}			nickname 用户昵称 
	* @apiParam (输入参数：) {string}			order_sn 订单号 
	* @apiParam (输入参数：) {string}			price 提现金额 
	* @apiParam (输入参数：) {int}				status 状态 待审核|0|warning,同意|1|success

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码  201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.msg 返回成功消息
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":"操作成功"}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" 201","msg":"操作失败"}
	*/
	function add(){
        $wxapp_id      = $this->request->post('wxapp_id');
        $uId           = $this->request->uid;
        //支付宝账号
        //$wxNum         = $this->request->post('wx_num', '', 'serach_in');
        //提现金额
        $withdrawalNum = $this->request->post('withdrawal_num', '', 'serach_in');
        //提现账号，用户手工输入
        $withdrawalNo = $this->request->post('withdrawal_no', '', 'serach_in');
        $payType       = $this->request->post('pay_type','','serach_in');
        file_put_contents('ZhWithdrawalExamine.txt', json_encode($this->_data));
        if(!$wxapp_id){
            return $this->ajaxReturn($this->errorCode,'缺少平台参数');
        }
        $wxSetting = SysSite::where('wxapp_id',$wxapp_id)->find();
        if(!$wxSetting){
            return $this->ajaxReturn($this->errorCode,'平台参数未配置');
        }
        
        $user = MemberModel::where(['uid' => $uId, 'wxapp_id' => $wxapp_id])->find();
        if(!$user){
            return $this->ajaxReturn($this->errorCode,'未知用户');
        }
		if(!$withdrawalNum){
            return $this->ajaxReturn($this->errorCode,'缺少提现金额');
        }
        if($withdrawalNum && $withdrawalNum > $user['balance']){
            return $this->ajaxReturn($this->errorCode,'提现金额不能大于余额');
        }
        if($withdrawalNum && $withdrawalNum < $wxSetting['min_withdrawal']){
            return $this->ajaxReturn($this->errorCode,'提现金额不能低于最低提现额度');
        }
        /*
		if(!$payType && $payType != 0){
            return $this->ajaxReturn($this->errorCode,'缺少提现方式');
        }
        if(!$wxNum && $payType == 0){
            return $this->ajaxReturn($this->errorCode,'缺少支付宝账号');
        }*/
        $nickName = $user['real_name'];
        if($nickName == ''){
            $nickName = $user['nickname'];
            if($nickName == ''){
                $nickName == $user['phone'];
            }
        }
		$data = [
		            'wxapp_id'  => $wxapp_id,
                    'u_id'      => $uId,
                    'nickname'  => $nickName,
                    'order_sn'  => date("Ymd") . rand(10000, 90000) . rand(10000, 90000),
                    'price'     => $withdrawalNum,
                    'status'    => 0,
                    'createtime'=> time(),
                    'withdrawal_no'  => $withdrawalNo,

        ];
		$r = ZhWithdrawalExamineModel::insertGetId($data);
		if($r){
            //查找新增的提现记录
            $ZWEData = ZhWithdrawalExamineModel::where([
                                                        'wxapp_id'=> $wxapp_id,
                                                        'u_id'    => $uId,
                                                        'id'      => $r
                                                    ])->find();
            //插入余额变更记录
            /*
            $newBalance = round($user['balance'] - $ZWEData['price'],2);
            ZhBalanceLogModel::insert([
                                        'u_id'         => $uId,
                                        'wxapp_id'     => $wxapp_id,
                                        'start_balance'=> $user['balance'],
                                        'end_balance'  => $newBalance,
                                        'type'         => '支出',
                                        'remake'       => '提现',
                                        'create_time'  => time(),
                                        'num'          => $ZWEData['price'],
                                        'order_sn'     => $ZWEData['order_sn']
            ]);
            //更新账户余额
            $res = MemberModel::where([
                                    'uid'      => $uId,
                                    'wxapp_id' => $wxapp_id
                                ])->update(['balance'=>$newBalance]);
            */
            ZhBalanceService::updateMoneyBalanceLog($wxapp_id, $uId, $ZWEData['price'], "支出", "提现", "");
        }
		return $this->ajaxReturn($this->successCode,'操作成功', "");
	}



}

